package com.hex.ds.hdrs.center.controller;

import com.alibaba.fastjson.JSON;
import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.ds.hdrs.center.service.CenterManager;
import com.hex.ds.hdrs.center.vo.GetMasterCenterRsp;
import com.hex.ds.hdrs.center.vo.HeartBeatReq;
import com.hex.ds.hdrs.center.vo.RegisterReq;
import com.hex.ds.hdrs.common.annotation.ServerLog;
import com.hex.ds.hdrs.common.base.BaseReq;
import com.hex.ds.hdrs.common.base.BaseRsp;
import com.hex.ds.hdrs.common.constant.HttpConst;
import com.hex.ds.hdrs.center.vo.CronTaskReq;
import com.hex.ds.hdrs.period.task.vo.CycleTaskReq;
import com.hex.ds.hdrs.period.task.vo.TaskHeartBeatReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/api/center")
@Api(tags = "中心操作")
public class CenterController {

    @Autowired
    private CenterManager centerManager;

    @ServerLog
    @AnonymousAccess
    @PostMapping("/register")
    @ApiOperation("节点注册")
    public ResponseEntity<Object> register(@RequestBody RegisterReq registerReq) {
        log.info("节点注册【CenterController.register】registerReq:{}", JSON.toJSONString(registerReq));
        BaseRsp rsp = centerManager.agentRegister(registerReq);
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

    @ServerLog
    @PostMapping("/masterUrl")
    @ApiOperation("获取master地址")
    @AnonymousAccess
    public ResponseEntity<Object> masterUrl(@RequestBody BaseReq baseReq) {
        log.info("获取master地址【CenterController.masterUrl】baseReq: {}", JSON.toJSONString(baseReq));
        BaseRsp rsp = new BaseRsp(HttpConst.HTTP_STATUS_OK, HttpConst.MASTER_URL_SUCC_MSG,
                centerManager.getMasterCenterIp(), new GetMasterCenterRsp(centerManager.getMasterCenterPort()));
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

    @ServerLog
    @PostMapping("/heartBeat")
    @ApiOperation("节点心跳")
    @AnonymousAccess
    public ResponseEntity<Object> heartBeat(@RequestBody HeartBeatReq heartBeatReq) {
        log.info("节点心跳【CenterController.heartBeat】heartBeatReq: {}", JSON.toJSONString(heartBeatReq));
        BaseRsp rsp = this.centerManager.heartBeat(heartBeatReq);
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

    @ServerLog
    @PostMapping("/taskHeartBeat")
    @ApiOperation("节点任务心跳")
    @AnonymousAccess
    public ResponseEntity<Object> taskHeartBeat(@RequestBody TaskHeartBeatReq taskHeartBeatReq) {
        log.info("节点任务心跳【CenterController.taskHeartBeat】taskHeartBeatReq.Ip: {}", taskHeartBeatReq.getIp());
        BaseRsp rsp = this.centerManager.taskHeartBeat(taskHeartBeatReq);
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

    @ServerLog
    @PostMapping("/cycleTaskExecRest")
    @ApiOperation("周期任务执行结果")
    @AnonymousAccess
    public ResponseEntity<Object> cycleTaskExecRest(@RequestBody CycleTaskReq cycleTaskReq) {
        log.debug("【开始】周期任务执行结果【CenterController.cycleTaskExecRest】cycleTaskReq: {}", JSON.toJSONString(cycleTaskReq));
        this.centerManager.cycleTaskExecBack(cycleTaskReq);
        BaseRsp rsp = new BaseRsp(HttpConst.HTTP_STATUS_OK, HttpConst.CYCLE_TASK_EXEC_BACK_SUCC_MSG,
                centerManager.getMasterCenterIp(), null);
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

    @ServerLog
    @PostMapping("/cronTaskExecRest")
    @ApiOperation("定时任务执行结果")
    @AnonymousAccess
    public ResponseEntity<Object> cronTaskExecRest(@RequestBody CronTaskReq cronTaskReq) {
        log.info("定时任务执行结果【CenterController.cronTaskExecRest】cronTaskReq: {}", JSON.toJSONString(cronTaskReq));
        this.centerManager.cronTaskExecBack(cronTaskReq);
        BaseRsp rsp = new BaseRsp(HttpConst.HTTP_STATUS_OK, HttpConst.CRON_TASK_EXEC_BACK_SUCC_MSG,
                centerManager.getMasterCenterIp(), null);
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

    @ServerLog
    @AnonymousAccess
    @PostMapping("/agentNotRegistor")
    @ApiOperation("节点未注册")
    public ResponseEntity<Object> agentNotRegistor(@RequestBody RegisterReq registerReq) {
        log.info("节点未注册，下线处理【CenterController.agentNotRegistor】registerReq: {}", JSON.toJSONString(registerReq));
        BaseRsp rsp = centerManager.agentNotRegistor(registerReq);
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

    @ServerLog
    @PostMapping("/masterInit")
    @ApiOperation("master初始化")
    @AnonymousAccess
    public ResponseEntity<Object> masterInit() {
        log.info("master初始化【CenterController.masterInit】========");
        centerManager.init();
        BaseRsp rsp = new BaseRsp();
        return new ResponseEntity<>(rsp, HttpStatus.OK);
    }

}
